package algorithms;

public class MyList {
	
	Node startNode;
	
	public MyList() {

	}
	
	public void add(int value) {
		if(startNode == null) {
			startNode = new Node(value);
		} else {
			Node currNode = startNode;
			while(currNode.nextNode != null)
				currNode = currNode.nextNode;
			
			currNode.nextNode = new Node(value);
		}
		
	}
	
	public int get(int i) {
		int value = -1;

		if(size() <= i) {
			return value;
		}
		int counter = 0;
		Node currNode = startNode;
		
		while(counter <= i) {
			value = currNode.value;
			currNode = currNode.nextNode;
			counter++;
		}
		
		return value;
	}
	
	public void set(int position, int value) {
		int counter = 0;
		Node currNode = startNode;
		
		while(counter <= position) {
			currNode.value = value;
			currNode = currNode.nextNode;
			counter++;
		}
	}
	
	public int size() {
		int size = 0;
		Node currNode = startNode;
		while(currNode != null) {
			size++;
			currNode = currNode.nextNode;
		}
		return size;
	}

	public void insert(int position, int value) {
		Node newNode = new Node(value);

		newNode.nextNode = getNode(position);
		getNode(position - 1).nextNode = newNode;
		
	}
	
	public void delete(int position) {
		getNode(position - 1).nextNode = getNode(position + 1);
	}
	
	private Node getNode(int i) {
		int counter = 0;
		Node currNode = startNode;
		while(counter < i) {
			currNode = currNode.nextNode;
			counter++;
		}
		return currNode;
	}
	
	private void setNode(int position, Node node) {
		
	}
	
	class Node {
		int value;
		Node nextNode;
		
		public Node(int value) {
			this.value = value;
		}
	}
	
}
